﻿using System;
using System.Xml;
using System.Xml.XPath;
using FeedsLibrary.Feeds.Abstract;

namespace FeedsLibrary.Feeds.RSS
{
    public class RssChannel : AbstractChannel
    {

        public RssChannel()
        {
            Days = 100;
            TagsOnly = false;
        }

        public RssChannel(Uri url, Uri iconUrl, DateTime currentDay, int days, bool tagsOnly)
        {
            CurrentDay = currentDay;
            Days = days;
            TagsOnly = tagsOnly;
            LoadDocumentFromUrl( url, iconUrl );
            LoadBody( false );
        }

        protected override void LoadBody( bool update )
        {
            if ( Document.DocumentElement != null )
            {
                XmlNode n = Document;

                n = FindElement( n, "rss" );

                n = FindElement( n, "channel" );

                LoadHeader( n );
                LoadItems( n, "item", update );
            }
            Document = null;
        }

        public override IItem GetNewItem(string domain, IXPathNavigable nodeItem, DateTime lastBuildDate)
        {
            return new RSSItem( domain, nodeItem, LastBuildDate );
        }
    }
}